#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>     
#include <termios.h>  

int main() 
{    
    int port;
    int count = 0;
    char buf[] = "\r\nPlease press any key... in 10 sec\r\n";
    struct termios tty_attributes;

    port = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
    tcgetattr(port, &tty_attributes);
    tty_attributes.c_cflag = CS8 | CLOCAL | CREAD; 
    tty_attributes.c_iflag = IGNPAR | IGNBRK;
    tty_attributes.c_oflag = 0;

    cfsetospeed(&tty_attributes,B9600);		// Set the baud rate
    cfsetispeed(&tty_attributes,B9600);
    tcflush(port, TCIOFLUSH);
    tcsetattr(port, TCSANOW, &tty_attributes);
	sleep(1);
	
	write(port, buf, sizeof(buf));
    sleep(10);

    while (read(port, &buf[count], 1) > 0) 
	{
		count++;
    }
    buf[count++] = 0;
    printf("Your pressed key: %s\n", buf);
    
	write(port, buf, sizeof(char) * count);
	
    close(port);
    exit(0);
}
